/**
 * 
 */
package com.zhaoyan.cms.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageInfo;
import com.zhaoyan.cms.pojo.Article;
import com.zhaoyan.cms.pojo.Category;
import com.zhaoyan.cms.pojo.Channel;
import com.zhaoyan.cms.pojo.Link;
import com.zhaoyan.cms.service.ArticleService;
import com.zhaoyan.cms.service.LinkService;

/**
 * @author zhaoyan
 *
 * 2020年1月18日
 */
@Controller
public class IndexController {
	
	
	@Autowired
	private ArticleService articleService; 
	
	
	@Autowired
	private LinkService linkService;

	/**
	 *启用线程来查询页面中的几个模块里的文章
	 * @param m
	 * @param pageNum
	 * @return
	 */
	@RequestMapping({"/","index"})
	public String showIndex(Model m , @RequestParam(defaultValue = "1") Integer pageNum) {
		
		//获取到热门的文章
		Thread findHotArticle = new Thread() {
			
			public void run() {
				
				PageInfo<Article> hotList = articleService.hotList(pageNum);
				m.addAttribute("hotList", hotList );
				
			};
			
		};
		//	获取最新的文章
		Thread findArticleByCreated = new Thread() {
			
			public void run() {
				
				List<Article> articleListByCreated = articleService.articleListByCreated(10);
				m.addAttribute("articleListByCreated", articleListByCreated);
			};
		};
		
		//	获取所有的频道
		Thread findChannels = new Thread() {
			
			public void run() {
				
				List<Channel> channelList = articleService.findChannels();
				m.addAttribute("channelList", channelList);
			};
		};
		//	找到友情链接
		Thread findLink = new Thread() {
			
			
			@Override
			public void run() {
				
				List<Link> links = linkService.findLinkList();
				System.out.println(links);
				m.addAttribute("links ", links );
				
			}
			
		};
		
		List<Link> links = linkService.findLinkList();
		m.addAttribute("links",links );
		
		
		
		findHotArticle.start();
		findArticleByCreated.start();
		findChannels.start();
		findLink.start();
		
		try {
			findHotArticle.join();
			findArticleByCreated.join();
			findChannels.join();
			findLink.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return "index";
	}
	
	/**
	 * 在文章列表中查看
	 * @param m
	 * @param pageNum 当前页
	 * @param categoryId 种类的id
	 * @param channelId	频道id
	 * @return
	 */
	@RequestMapping("articleShow")
	public String showArticlList(Model m ,
			@RequestParam(defaultValue = "1") Integer pageNum ,
			@RequestParam(defaultValue = "1") Integer categoryId ,
			@RequestParam(defaultValue = "1") Integer channelId) {
		
		// 获取到当前频道下的文章
		Thread findArticleList = new Thread() {
			public void run() {
				
				PageInfo<Article> articlePage = articleService.articleList(categoryId,channelId,pageNum);
				System.out.println("articlePage" +articlePage.getList().size());
				m.addAttribute("articlePage",articlePage );
			};
		};
		//	获取最新的文章
			Thread findArticleByCreated = new Thread() {
				
				public void run() {
					
					List<Article> articleListByCreated = articleService.articleListByCreated(10);
					m.addAttribute("articleListByCreated", articleListByCreated);
				};
			};		
		// 获取所有的频道
			Thread findChannels = new Thread() {
				
				public void run() {
					
					List<Channel> channelList = articleService.findChannels();
					m.addAttribute("channelList", channelList);
				};
			};
			
		//	获取当前频道下的文章类别
			Thread findCategoryList = new Thread() {
				
				public void run() {
					
					List<Category> categoryList = articleService.findCategoryList(channelId);
					m.addAttribute("categoryList",categoryList );
				};
			};
			
			findArticleList.start();
			findArticleByCreated.start();
			findChannels.start();
			findCategoryList.start();
			
			try {
				findArticleList.join();
				findArticleByCreated.join();
				findChannels.join();
				findCategoryList.join();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			m.addAttribute("channelId",channelId );
			m.addAttribute("categoryId",categoryId );
			
			
		return "articles";
	}
	
	
}
